@include('public.page_styles')
{{--<link rel="stylesheet" href="{{ asset("/AdminLTE/plugins/iCheck/all.css") }}">--}}
<style>
  .checkbox label, .radio label {
    padding-left: 0px;
  }
</style>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
@include('public.top')
  <!-- Left side column. contains the logo and sidebar -->
@include('public.sidebar')
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        {{ trans('message.rolesTitle') }}
        <small> {{ trans('message.edit') }}</small>
      </h1>
      <ol class="breadcrumb">
        <li><a ><i class="fa fa-dashboard"></i>  {{ trans('message.home') }}</a></li>
        <li><a href="{{ url('roles/list') }}"> {{ trans('message.rolesTitle') }} {{ trans('message.list') }}</a></li>
        <li class="active">  {{ trans('message.rolesTitle') }}{{ trans('message.edit') }}</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <div class="row">
        <div class="col-md-12">
          <!-- Horizontal Form -->
          <div class="box box-info">
            <div class="box-header with-border">
              <h3 class="box-title"> {{ trans('message.edit') }}</h3>
              <div class="box-tools">
                <div class="btn-group pull-right" style="margin-right: 10px">
                  <a href="{{ url('roles/list') }}" class="btn btn-sm btn-default"><i class="fa fa-list"></i>&nbsp; {{ trans('message.list') }}</a>
                </div>
                <div class="btn-group pull-right" style="margin-right: 10px">
                  <a class="btn btn-sm btn-default form-history-back" onclick="goBack()"><i class="fa fa-arrow-left"></i>&nbsp; {{ trans('message.history-back') }}</a>
                </div>
              </div>
            </div>
            <!-- /.box-header -->
            <!-- form start -->
            <form name="editForm" action="{{ url('roles/'.$rolesInfo->id) }}" method="post"  class="form-horizontal">
              {{--{{ csrf_field() }}--}}
              <input type="hidden" name="_token"   value="{{ csrf_token() }}"/>

              <input type="hidden" name="role_id"   value="{{ $rolesInfo->id }}"/>

              <div class="box-body">
                <div class="form-group {!! !$errors->has('slug') ?: 'has-error' !!}">
                  <label for="slug" class="col-sm-2 control-label">标识</label>
                  <div class="col-sm-8">
                    <span class="form-control-require">*</span>
                    <input type="text" class="form-control" id="slug" name="slug" placeholder="slug" value="{{ $rolesInfo->slug}}"   autofocus>
                    @if($errors->has('slug'))
                      @foreach($errors->get('slug') as $message)
                        <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label></br>
                      @endforeach
                    @endif
                  </div>
                </div>

                <div class="form-group {!! !$errors->has('role_name') ?: 'has-error' !!}">
                  <label for="role_name" class="col-sm-2 control-label">角色名</label>
                  <div class="col-sm-8">
                    <span class="form-control-require">*</span>
                    <input type="text" class="form-control" id="role_name" name="role_name" placeholder="角色名" value="{{ $rolesInfo->role_name}}"   >
                    @if($errors->has('role_name'))
                      @foreach($errors->get('role_name') as $message)
                        <label class="control-label" for="inputError"><i class="fa fa-times-circle-o"></i>{{$message}}</label></br>
                      @endforeach
                    @endif
                  </div>
                </div>

                <div class="form-group">
                  <label for="inputEmail3" class="col-sm-2 control-label">权限分配</label>
                  <div class="col-sm-8">
                    <a href="javascript:void(0);" class="permission-select-all"
                       id="selectAll">全选</a> / <a href="javascript:void(0);"
                                                  class="permission-deselect-all"
                                                  id="cancelAll">全消</a>
                    <ul class="permissions menu_parent checkbox">
                      <input type="hidden" value="1" name="menuPermission[]" checked>
                      @if($menus)
                        @foreach($menus as $menu)
                          @if($menu->id !=1)
                            @if($menu->parent_id == $menu->id )
                              <li class="outer">
                                @if($roleMenus)
                                  <input type="checkbox" value="{{$menu->id}}"
                                         name="menuPermission[]"
                                         @if(in_array($menu->id, $roleMenus)) checked @endif>
                                @else
                                  <input type="checkbox" value="{{$menu->id}}"
                                         name="menuPermission[]">
                                @endif
                                <label for="{{$menu->title}}"><strong>{{$menu->title}}</strong></label>
                                <ul>
                                  @if($itemO = menu('admin',$menu->parent_id))
                                    @foreach($itemO as $key=>$value)
                                      @if($key !=0)
                                        <li class="inner menu_oup">
                                          @if($roleMenus)
                                            <input type="checkbox"
                                                   value="{{ $value['id'] }}"
                                                   name="menuPermission[]"
                                                   @if(in_array($value['id'], $roleMenus)) checked @endif>
                                          @else
                                            <input type="checkbox"
                                                   value="{{ $value['id'] }}"
                                                   name="menuPermission[]">
                                          @endif
                                          <label for="{{$value['title']}}"><strong>{{$value['title']}}</strong></label>

                                          {{--功能操作--}}
                                          <ul>
                                            @foreach($menuOption as $key=>$optionName)
                                              @if($value['id'] == $optionName->menu_id)
                                                <li class="menu_inp"
                                                    style="float:left;margin-right:40px;">
									                <?php
									                $arr = getOneMenuRoleOps($rolesInfo->id, $optionName->menu_id);
									                ?>
                                                  @if($arr)
                                                    <input type="checkbox"
                                                           value="{{$optionName->option_id}}"
                                                           name="menuOptions[{{$optionName->menu_id}}][]"
                                                           @if(in_array($optionName->option_id, $arr)) checked @endif>
                                                  @else
                                                    <input type="checkbox"
                                                           value="{{$optionName->option_id}}"
                                                           name="menuOptions[{{$optionName->menu_id}}][]"/>
                                                  @endif
                                                  <label>{{ $optionName->option_name }}</label>
                                                </li>
                                              @endif
                                            @endforeach
                                          </ul>
                                          <br>
                                        </li>
                                      @endif
                                    @endforeach
                                  @endif
                                </ul>
                              </li>
                            @endif
                          @endif
                        @endforeach
                      @endif
                    </ul>
                  </div>
                </div>

              </div>
              <!-- /.box-body -->
              <div class="box-footer">
                <div class="col-sm-2">
                </div>
                <div class="col-sm-8">
                  <div class="btn-group pull-right">
                    <button type="button" class="btn btn-info pull-right btn-js-form-submit" >提交</button>
                  </div>
                  <div class="btn-group pull-left">
                    <button type="reset" class="btn btn-warning">撤销</button>
                  </div>
                </div>
              </div>
              <!-- /.box-footer -->
            </form>
          </div>
          <!-- /.box -->
        </div>
        <!--/.col (right) -->
      </div>
      <!-- /.row -->
    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
@include('public.bottom')

  <!-- Control Sidebar -->
{{--@include('public.aside')--}}
  <!-- /.control-sidebar -->
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

@include('public.javascript')

<script type="text/javascript">

    jQuery(function ($) {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': '{{ csrf_token() }}'
            }
        });


        @if(session()->has('fail'))
          alertMess("{{session()->get('fail')}}");
        @endif

    });

    $(document).ready(function () {
        //全选
        $("#selectAll").click(function () {
            $(".menu_parent :checkbox").attr("checked", true);
        });
        // 全不选
        $("#cancelAll").click(function () {
            $(".menu_parent :checkbox").attr("checked", false);
        });

        $(".menu_parent li input:checkbox").click(function () {
            if ($(this).parent().hasClass('outer')) {
                var _li_outer = $(this).parent();
                if (_li_outer.find(".inner input:checked").prop("checked")) {
                    _li_outer.find(".inner >input").prop("checked", false);
                } else {
                    // > 指的是:选中当前的子节点 checked，而不是后边所有input
                    _li_outer.find(".inner >input").prop("checked", true);
                }
            } else {
                var _li_inner = $(this).parent().parent().parent();
                if (_li_inner.find(".inner input:checked").prop("checked")) {
                    _li_inner.find(">input").prop("checked", true);
                } else {
                    _li_inner.find(">input").prop("checked", false);
                }
            }


            if ($(this).parent().hasClass('menu_oup')) {
                // 选择菜单，勾选所有功能
//                alert(1111111);
//                var _li_oup = $(this).parent();
//                var _li_oup_outer = $(this).parent().parent().parent(); // 父级别outer
//                if (_li_oup.find(".menu_inp input:checked").prop("checked")) {
//                    _li_oup.find(".menu_inp input").prop("checked", false);
//                    _li_oup_outer.find(">input").prop("checked", false); // 父
//
//                } else {
//                    _li_oup.find(".menu_inp input").prop("checked", true);
//                    _li_oup_outer.find(">input").prop("checked", true); // 父
//                }
            } else {
                // 选择功能，自动勾选对应菜单
                var _li_inp = $(this).parent().parent().parent();
                var _li_inp_in = $(this).parent().parent().parent().parent().parent();// 父

                if (_li_inp.find(".menu_inp input:checked").prop("checked")) {
                    _li_inp.find(">input").prop("checked", true);
                    _li_inp_in.find(">input").prop("checked", true); // 父

                } else {
                    _li_inp.find(">input").prop("checked", false);
//                    _li_inp_in.find(">input").prop("checked", false); // 父

                }
            }


//            $(this).closest('ul').find(" ul" + classN + "input:checkbox").prop("checked",$(this).prop("checked"));
        });

    });


    var _ajaxSubmitSending = false;
    $(".btn-js-form-submit").click(function (e) {
        if (_ajaxSubmitSending) {
            return;
        } else {
            _ajaxSubmitSending = true;
            _loadingFuc();
        }

        $('form[name="editForm"]').submit();
        _ajaxSubmitSending = false;
        setTimeout(function(){
            $.unblockUI();
        }, 1500);
    });




</script>



</body>
</html>
